<?php
/**
 * 《我的支出》实现类
 *
 * @author 孙晓晔
 * @version $Id: Work.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/Out.class.php');

class OutMy extends Out {

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}

	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-1 year'));

		$controller->display($request, 'finance/out/my/find');
		return true;
	}
	
	function _my() {		
		$emp = $_SESSION['id'];
		
		return sprintf(" visible = 1 AND (emp = '%s' OR oper = '%s')", $emp, $emp);
	}
	
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);
				
				$row['num_'] = $this->_num($row['id']);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$row['is_delete'] = $this->_is_delete($row);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'finance/out/my/list');
		return true;
	}
	
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('t1', strtotime('-3 day'));
		
		$controller->display($request, 'finance/out/my/add');
		return true;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);
						
		$this->_add($controller, $request);
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _add($controller, $request) {
		$supplier = $this->_input($request['supplier']);
		$supplier_x = addslashes($request['supplier_x']);
		
		$mold = $this->_input($request['mold']);
		$name = addslashes($request['name']);
		$type = $request['type'];
		
		$currency = $request['currency'];
		$amount = $request['amount'];
		$rate = $request['rate'];
		$rmb = $request['rmb'];
		
		$date = $request['date'];	
		$content = addslashes($request['content']);
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, supplier, supplier_x, name, mold, type, currency, amount, rate, rmb, date, content, emp, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $supplier, $supplier_x, $name, $mold, $type, $currency, $amount, $rate, $rmb, $date, $content, $emp, $_SESSION['id']);
		debug($sql);
		$db->query($sql);		
		$id = $db->insert_id();
		
		$a = array();
		$a['name'] = '(1)';
		$a['currency'] = $currency;
		$a['total'] = $amount;
		$a['content'] = $content;
		$a['ok'] = true;
		
		$item = array();
		$item[] = $a;
		
		$this->_insert_out_item($id, $item, $supplier, $emp, $mold);
		
		return $id;
	}
	
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/out/my/edit');
		return true;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);
				
		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
		
		$supplier = $this->_input($request['supplier']);
		$supplier_x = addslashes($request['supplier_x']);

		$mold = $this->_input($request['mold']);
		$name = addslashes($request['name']);
		$type = $request['type'];
		
		$currency = $request['currency'];
		$amount = $request['amount'];
		$rate = $request['rate'];
		$rmb = $request['rmb'];
		
		$date = $request['date'];		
		$content = addslashes($request['content']);
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', supplier = '%s', supplier_x = '%s', name = '%s', mold = '%s', type = '%s', currency = '%s', amount = '%s', balance = '%s', rate = '%s', rmb = '%s', date = '%s', content = '%s', emp = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $supplier, $supplier_x, $name, $mold, $type, $currency, $amount, $balance, $rate, $rmb, $date, $content, $emp, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/out/my/copy');
		return true;
	}
	
	function upload($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		
		$this->_files_clean($id);
		
		$row = $this->_view($id);
		
		$row['files'] = $this->_files($row['id']);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'finance/out/my/upload');
		return true;
	}
}
?>
